<template>
	<view class="account-page">
		<view class="account-info">
			<view class="account-avatar-nickname">
				<view class="avatar-image">
					<image class="icon-avatar" :src="userInfo.avatar"></image>
				</view>
				<view class="nickname-txt">{{ userInfo.tel }}</view>
			</view>
			<view class="account-assets">
				<!-- #ifndef MP-ALIPAY -->
				<view
					class="account-balance"
					@click="jumpWallet"
				>
					<view class="num">{{ balanceInfo.totalAmount / 100 || 0 }}</view>
					<view class="title">余额</view>
				</view>
				<!-- #endif -->
				<view
					class="account-coupon"
					@click="jumpCoupon"
				>
					<view class="num">{{ balanceInfo.couponCount || 0 }}</view>
					<view class="title">优惠券</view>
				</view>
			</view>
		</view>
		<view class="account-body">
			<view class="sale-orders">
				<hsb-common-base-menu-list
					title="我卖出的"
					iconSize="64rpx"
					:list="saleList"
					@jump="jumpPage"
				/>
			</view>
			<!-- #ifndef MP-ALIPAY -->
			<view class="sell-orders">
				<hsb-common-base-menu-list
					title="我买到的"
					:list="sellList"
					@jump="jumpKele"
				/>
			</view>
			<!-- #endif -->
			<view class="common-function">
				<hsb-common-base-menu-list
					title="常用功能"
					:list="funList"
					@jump="jumpPage"
				/>
			</view>
			<view class="tool-box">
				<hsb-common-base-menu-list
					title="工具箱"
					:list="toolList"
					@jump="jumpPage"
				/>
			</view>
		</view>
		<!-- #ifdef APP-PLUS -->
		<hsb-common-base-confirm
			ref="accountBaseConfirm"
			title="提示"
			contentAlign="center"
			content="回收宝想打开“小豹帮卖”小程序"
			confirmText="好的"
			@confirm="jumpMiniprogram"
		/>
		<!-- #endif -->
	</view>
</template>

<script>
import { mapState } from 'vuex'
import { saleList, sellList, funList, toolList, cocoAppId } from '@/config'
	
export default {
	computed: {
		...mapState({
			userInfo: state => state.user.userInfo,
			balanceInfo: state => state.user.balanceInfo,
		})
	},
	data () {
		return {
			saleList,
			sellList,
			funList,
			toolList,
			miniprogramInfo: {}
		}
	},
	methods: {
		jumpPage (pageInfo) {
			// #ifdef APP-PLUS
			if (pageInfo.type === 'miniprogram') {
				this.miniprogramInfo = pageInfo
				return this.$refs.accountBaseConfirm.open()
			}
			// #endif
			this.$store.dispatch('openPage', pageInfo)
		},
		jumpWallet () {
			uni.navigateTo({
				url: '/pages/wallet/index'
			})
		},
		jumpCoupon () {
			this.jumpPage({
				redirectUrl: 'https://ka.huishoubao.com/hsbh5/coupons'
			})
		},
		// #ifdef APP-PLUS
		jumpMiniprogram () {
			plus.share.getServices(serviceList => {
				serviceList.forEach(item => {
					if (item.id !== 'weixin') return
					item.launchMiniProgram({
						id: this.miniprogramInfo.id
					})
				})
			})
		},
		// #endif
		async jumpKele (pageInfo) {
			const [res] = await this.$apis.getKeleOauthCode()
			if (res._errCode === '0' && res._data?.code) {
				const url = `${pageInfo.redirectUrl}&code=${res._data.code}`
				this.jumpPage({
					// #ifdef MP-WEIXIN
					redirectUrl: `othermini?appid=${cocoAppId}&path=/pages/h5/main?url=${encodeURIComponent(url)}`
					// #endif
					// #ifndef MP-WEIXIN
					redirectUrl: url
					// #endif
				})
			} else {
				uni.showToast({
					title: res._errStr || '获取跳转信息失败',
					icon: 'none'
				})
			}
		}
	},
	onShow () {
		this.$apis.checkLoginStatus().then(([res]) => {
			if (res._errCode === '0') {
				if (Number(res._data.status) === 1) {
					this.$store.dispatch('user/getUserInfo')
					this.$store.dispatch('user/getBalanceInfo')
				}
			}
		})
	}
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
